package test.serverframe.armc.server.manager.service.impl;/**
 * Created by luoxuan on 2018/10/23.
 */

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.TypeReference;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import test.serverframe.armc.server.manager.enums.HttpMethodEnum;
import test.serverframe.armc.server.manager.model.CenterInterface;
import test.serverframe.armc.server.manager.service.CenterAPIService;
import test.serverframe.armc.server.manager.service.ServerApiService;
import test.serverframe.armc.server.util.JsonUtil;
import test.serverframe.armc.server.util.RestTemplateUtil;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

/**
 * @Description 远程服务调用接口实现类
 * @Author LSY
 * @Date 2018/10/23 9:21
 * @ClassName test.serverframe.armc.server.manager.service.impl.ServerApiServiceImpl
 */
@Service("serverApiService")
public class ServerApiServiceImpl implements ServerApiService {

    @Autowired
    RestTemplate restTemplate;
    
    @Autowired
    CenterAPIService centerAPIService;
    
    @Autowired
    private ServerApiService serverApiService;

    /**
     * 服务
     *
     * @param pageNum
     * @param pageSize
     * @param sName
     * @return
     * @throws JSONException
     */
    @Override
    public JSONArray listServer(int pageNum, int pageSize, String sName) throws JSONException {
        String jsonStr;
        //判断是否根据服务名称筛选查询
        if (StringUtils.isNotBlank(sName)) {
            jsonStr = "{sName:\"" + sName + "\"}";
        } else {
            jsonStr = "{}";
        }
        String url = "http://188.88.30.120:11101/microService/pages?pageNum=" + pageNum + "&pageSize=" + pageSize;
        //远程服务调用
        Object ob = RestTemplateUtil.getResponseEntity(HttpMethod.POST, url, "{}", jsonStr, "{}");
        return JsonUtil.getJSONArrayCtList(ob);
    }

    /**
     * 实例
     *
     * @param sId
     * @return
     * @throws JSONException
     */
    @Override
    public JSONArray listInstance(String sId) throws JSONException {
        //远程服务调用
        Object ob = RestTemplateUtil.getResponseEntity(HttpMethod.POST, "http://188.88.30.120:11101/microService/instanceList?sId=" + sId, "{}", "{}", "{}");
        return JsonUtil.getJSONArray(ob);
    }

    /**
     * 类别
     *
     * @param pageNum
     * @param pageSize
     * @param siId
     * @param siaoName
     * @return
     * @throws JSONException
     */
    @Override
    public JSONArray listApiOwer(int pageNum, int pageSize, String siId, String siaoName) throws JSONException {
        String jsonStr;
        //判断是否根据类别名称筛选查询
        if (StringUtils.isNotBlank(siaoName)) {
            jsonStr = "{siId:" + siId + ",siaoName:\"" + siaoName + "\"}";
        } else {
            jsonStr = "{siId:" + siId + "}";
        }
        //远程服务
        //
        //
        // 调用
        Object ob = RestTemplateUtil.getResponseEntity(HttpMethod.POST, "http://188.88.30.120:11101/microServiceInstanceApi/apiOwerPages?pageNum=" + pageNum + "&pageSize=" + pageSize, "{}", jsonStr, "{}");
        return JsonUtil.getJSONArrayCtList(ob);
    }

    /**
     * 接口
     *
     * @param siId   实例ID
     * @param siaoId 类别ID
     * @return
     * @throws JSONException
     */
    @Override
    public JSONArray listInterface(String siId, String siaoId) throws JSONException {
        //远程服务调用
        Object ob = RestTemplateUtil.getResponseEntity(HttpMethod.POST, "http://188.88.30.120:11101/microServiceInstanceApi/apiList?siId=" + siId + "&siaoId=" + siaoId, "{}", "{}", "{}");
        return JsonUtil.getJSONArray(ob);
    }

    /**
     * @param siaId 接口id
     * @return
     */
    @Override
    public CenterInterface getInterface(String siaId) {
        String json = centerAPIService.readResource("interface/" + siaId + ".json");
        return JSON.parseObject(json, new TypeReference<CenterInterface>() {
        });
    }

    /**
     * @param siaId     接口ID
     * @param queryStr  Query类型参数
     * @param bodyStr   Body类型参数
     * @param headerStr Header类型参数
     * @return 响应参数的Object对象
     * @throws JSONException
     * @Author UaenaSA
     * 传入接口ID 和请求参数得到接口响应参数
     */
    @Override
    public String getInterfaceResponse(String siaId, String queryStr, String bodyStr, String headerStr) throws JSONException {
        CenterInterface centerInterface = serverApiService.getInterface(siaId);
        String url = centerInterface.getSiIpAddress() + ":" + centerInterface.getSiPort() + centerInterface.getSiaPath();
        String result = RestTemplateUtil.getResponseEntity(HttpMethodEnum.getMethod(centerInterface.getSiaHttpMethod()), url, queryStr, bodyStr, headerStr);
        return result;
    }
}
